package sp.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sp.dao.book.BookCategoryDAO;
import sp.pojo.BookCategory;
import sp.service.BookCategoryService;

@Service("bookCategoryService")
public class BookCategoryServiceImpl implements BookCategoryService{

	
	@Autowired
	private BookCategoryDAO bookCategoryDao;
	
	@Override
	public List<BookCategory> listBookCatgorys(Integer parentId) throws Exception{
		return bookCategoryDao.listBookCatgorys(parentId);
	}

	@Override
	public List<BookCategory> listAllCategorys() throws Exception{
		return bookCategoryDao.listAllCategorys();
	}

	@Override
	public LinkedHashMap<BookCategory, LinkedHashMap<BookCategory, List<BookCategory>>> listCategoryWithMap() throws Exception{
		
		List<BookCategory> bookLevel1List = listBookCatgorys(null);
		if (null == bookLevel1List || bookLevel1List.size() == 0)
			return null;
		
		/**
		 * 第一个key: 第一级目录
		 */
		LinkedHashMap<BookCategory, LinkedHashMap<BookCategory, List<BookCategory>>> map = new LinkedHashMap<>();
		
		for (BookCategory bookCategory : bookLevel1List) {
			
			LinkedHashMap<BookCategory, List<BookCategory>> map2 = new LinkedHashMap<>();
			List<BookCategory> bookLevel2List = listBookCatgorys(bookCategory.getId());
			
			for (BookCategory bookCategory2 : bookLevel2List) {
				List<BookCategory> bookLevel3List = listBookCatgorys(bookCategory2.getId());
				List<BookCategory> list = new ArrayList<>();
				
				for (BookCategory bookCategory3 : bookLevel3List) {
					list.add(bookCategory3);
				}
				
				map2.put(bookCategory2, list);
			}
			
			map.put(bookCategory, map2);
		}
		
		
		return map;
	}

}
